/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utn.tsb.tej.front_end;

import com.trolltech.qt.gui.QCommandLinkButton;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QVBoxLayout;
import utn.tsb.tej.comunications.RequestResult;
import utn.tsb.tej.comunications.client.ClientRequests;
import utn.tsb.tej.world_logic.Player;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class JoinGameWindow extends QDialog {

    QLineEdit txtURL, txtPassword;
    QPushButton btnCheckURL, btnJoin, btnCancel;

    public JoinGameWindow ()
    {
        setWindowTitle("T.E.J. - Join Room Game");
        setFixedSize(400, 160);

        QVBoxLayout mainLayout = new QVBoxLayout(this);

        QHBoxLayout addressLayout = new QHBoxLayout();

        txtURL = new QLineEdit();
        txtURL.textChanged.connect(this, "txtURL_textChanged()");
        addressLayout.addWidget(txtURL);

        btnCheckURL = new QCommandLinkButton("Check IP");
        btnCheckURL.clicked.connect(this, "btnCheckURL_clicked()");
        btnCheckURL.setMaximumWidth(100);
        btnCheckURL.setEnabled(false);
        addressLayout.addWidget(btnCheckURL);

        txtPassword = new QLineEdit();
        txtPassword.setEchoMode(QLineEdit.EchoMode.Password);

        btnJoin = new QPushButton("Join Room");
        btnJoin.clicked.connect(this, "btnJoin_clicked()");
        btnJoin.setEnabled(false);
        btnCancel = new QPushButton("Cancel");
        btnCancel.clicked.connect(this, "btnCancel_clicked()");

        QHBoxLayout acceptLayout = new QHBoxLayout();

        acceptLayout.addWidget(btnJoin);
        acceptLayout.addWidget(btnCancel);

        mainLayout.addWidget(new QLabel("Server Address:"));
        mainLayout.addLayout(addressLayout);
        mainLayout.addWidget(new QLabel("Room Password:"));
        mainLayout.addWidget(txtPassword);
        mainLayout.addLayout(acceptLayout);
    }

    private void btnCheckURL_clicked()
    {
        RequestResult rResult = new ClientRequests(txtURL.text()).requestGameData();
        new QMessageBox(rResult.getIcon(),
                rResult.getTitle(),
                rResult.getText()).show();
    }

    private void txtURL_textChanged()
    {
        boolean enable = !(txtURL.text().compareToIgnoreCase("") == 0);
        btnCheckURL.setEnabled(enable);
        btnJoin.setEnabled(enable);
    }

    private void btnCancel_clicked()
    {
        this.dispose();
    }

    private void btnJoin_clicked()
    {
        String address = txtURL.text();
        this.dispose();
        WaitingWindow waitWindow = new WaitingWindow("Joining Room Game",
                "Connecting to the Server.\nThis could take a few seconds...");
        waitWindow.show();
        RequestResult rResult = new ClientRequests(address).requestJoinGame();
        if(rResult.isSuccess())
        {
            waitWindow.dispose();
            new RoomGameWindow((Player)rResult.getRequestObject(),
                    address).show();
        }
        else
        {
            waitWindow.dispose();
            new QMessageBox(rResult.getIcon(), rResult.getTitle(), rResult.getText()).show();
        }
    }

}
